package com.jyniubi.core.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyniubi.base.exception.Assert;
import com.jyniubi.base.exception.BussinessException;
import com.jyniubi.base.result.R;
import com.jyniubi.base.result.ResponseEnum;
import com.jyniubi.core.pojo.entity.IntegralGrade;
import com.jyniubi.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Api(tags = "积分等级管理模块")
@Slf4j

@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;

    /**
     * 查询所有积分等级
     * @return
     */
    @ApiOperation(value = "查询所有积分等级")
    @GetMapping
    public R list(){
        List<IntegralGrade> list = integralGradeService.list();
        Assert.assertObjectIsNull(list,ResponseEnum.INTEGRALGRADELIST_IS_NULL);
        return R.ok().data("items",list);
    }

    /**
     * 根据id查询对应的积分等级数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询对应的积分等级数据")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id")String id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        Assert.assertObjectIsNull(integralGrade,ResponseEnum.INTEGRALGRADE_IS_NULL);
        return R.ok().data("item",integralGrade);
    }

    /**
     * 根据id删除对应的积分等级数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除对应的积分等级数据")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")String id){
        boolean b = integralGradeService.removeById(id);
        Assert.assertNotTrue(b,ResponseEnum.INTEGRALGRADE_DELETE_FALSE);
        return R.ok();
    }

    /**
     *  分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "积分等级分页查询")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize){
        Page<IntegralGrade> page = integralGradeService.page(new Page<>(pageNum, pageSize));
        return R.ok().data("page",page);
    }

    /**
     * 新增
     * @param integralGrade
     * @return
     */
    @ApiOperation(value = "积分等级新增")
    @PostMapping
    public R save(@RequestBody IntegralGrade integralGrade) {
        if (StringUtils.isEmpty(integralGrade.getBorrowAmount())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_BORROWAMOUNT_EMPTY);
        }
        if (StringUtils.isEmpty(integralGrade.getIntegralStart())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_INTEGRALSTART_EMPTY);
        }
        if (StringUtils.isEmpty(integralGrade.getIntegralEnd())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_INTEGRALEND_EMPTY);
        }
        boolean b = integralGradeService.save(integralGrade);
        Assert.assertNotTrue(b,ResponseEnum.INTEGRALGRADE_INSERT_FALSE);
        return R.ok();
    }
    @ApiOperation(value = "积分等级批量删除")
    @DeleteMapping("/batchDel")
    public R batchDel(@RequestBody List<String> ids){
        boolean b = integralGradeService.removeByIds(ids);
        Assert.assertNotTrue(b,ResponseEnum.INTEGRALGRADE_BATCHDELETE_FALSE);
        return R.ok();
    }
    @ApiOperation("积分等级更新")
    @PutMapping("/update")
    public R update(@RequestBody IntegralGrade integralGrade) {
        if (StringUtils.isEmpty(integralGrade.getBorrowAmount())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_BORROWAMOUNT_EMPTY);
        }
        if (StringUtils.isEmpty(integralGrade.getIntegralStart())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_INTEGRALSTART_EMPTY);
        }
        if (StringUtils.isEmpty(integralGrade.getIntegralEnd())){
            throw new BussinessException(ResponseEnum.INTEGRALGRADE_INTEGRALEND_EMPTY);
        }
        boolean update = integralGradeService.updateById(integralGrade);
        Assert.assertNotTrue(update,ResponseEnum.INTEGRALGRADE_UPDATE_FALSE);
        return R.ok();
    }

}

